package com.guo.hui.controller;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.guo.hui.dao.blog.TpDictMapper;
import com.guo.hui.enumtype.DelFlag;
import com.guo.hui.model.blog.TpDict;
import com.guo.hui.service.DictService;
import com.guo.hui.util.UtilJson;

@Controller
@RequestMapping("/dict")
public class DictController {

	@Autowired
	private DictService dictservice;
	@Autowired
	private TpDictMapper dictDao;

	@RequestMapping("/index")
	public String index() {
		return "blog/dict/index";
	}

	@RequestMapping("/dictadd")
	public String dictadd() {
		return "blog/dict/dictadd";
	}

	@ResponseBody
	@RequestMapping("/dictsave")
	public String dictsave(@RequestParam String dict) {
		TpDict tp = (TpDict) UtilJson.fromJson(dict, TpDict.class);
		dictservice.saveDict(tp);
		return "ok";
	}

	@ResponseBody
	@RequestMapping("/dictdata")
	public String dictdata(Model mm) {
		List<TpDict> list = dictservice.selectByMap(new HashMap<String, Object>());
		return UtilJson.toJson(list);
	}

	@ResponseBody
	@RequestMapping("/dictdelete")
	public String dictdelete(@RequestParam int id) {
		TpDict tp = new TpDict();
		tp.setId(id);
		tp.setDelflag(DelFlag.AVIABLE.toString());
		dictservice.updateDict(tp);
		return "ok";
	}

	@RequestMapping("/update")
	public String update(Model mm, @RequestParam int id) {
		TpDict tp = dictDao.selectById(id);
		mm.addAttribute("tp", tp);
		return "blog/dict/update";
	}

	@ResponseBody
	@RequestMapping("/dictupdate")
	public String dictupdate(@RequestParam String tp) {
		TpDict dict = (TpDict) UtilJson.fromJson(tp, TpDict.class);
		dictservice.updateDict(dict);
		return "ok";
	}

}
